package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.PortForm;
import com.vcaresolution.logistic.to.PortTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>PortController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class PortController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.PORTFORM_MODELATTRIBUTE)
	public PortForm createForm(){
		return new PortForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Port Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_PORT_URL,method = RequestMethod.GET)
	public ModelAndView loadAddPort() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_PORT_VIEW);
		modelAndView.addObject(ApplicationConstants.PORTFORM_MODELATTRIBUTE, new PortForm());
		
/*		//Get Sector Master List
		modelAndView.addObject(ApplicationConstants.SECTORS_MODELATTRIBUTE, getServiceRegistry().getSectorService().findByAll());
*/		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Port Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_PORT_URL,method = RequestMethod.POST)
	public ModelAndView processAddPort(@Valid PortForm portForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			portForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getPortService().add(portForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_PORT_URL, true, true, false));
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the List Port Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_PORT_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<PortTO> portList = getServiceRegistry().getPortService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.PORTLIST_MODELATTRIBUTE, portList);
		modelAndView.setViewName(ApplicationConstants.LIST_PORT_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the Edit Port Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_PORT_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditPort(@RequestParam(value="portId") String portId) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object portObject = getServiceRegistry().getPortService().findById(Integer.parseInt(portId));
		
		modelAndView.addObject(ApplicationConstants.PORTFORM_MODELATTRIBUTE, portObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_PORT_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the Edit Port Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_PORT_URL,method = RequestMethod.POST)
	public ModelAndView processEditPort(@Valid PortForm portForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			portForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getPortService().update(portForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_PORT_URL, true, true, false));
		}
		
		return modelAndView;
	}
}